/*
 * @(#)MagicTextboxTag
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.Utility;

/**
  MagicTextboxTag.java
  
  @author      Jim Zhou
  @company     DCIVision Limited
  @creation date   2007/6/21
  @version     $Revision: 1.1.2.4 $
*/

public class MagicTextboxTag extends BodyTagSupport {

  public static final String REVISION = "$Revision: 1.1.2.4 $";
  
  private String maxlength = null;
  private String name = null;
  private String readonly = null;
  private String size = null;
  private String svalue = null;
  private String rows = null;
  private String cols = null;
  private String boxStyleClass = null;
  private String areaStyleClass = null;

  public MagicTextboxTag() {
    super();
  }
  
  public int doStartTag() throws JspException {
    if (Utility.isEmpty(maxlength)){
      maxlength = "";
    }
    if (Utility.isEmpty(rows)){
      rows = "3";
    }
    if (Utility.isEmpty(size)){
      size = "";
    }
    if (Utility.isEmpty(cols)){
      cols = "";
    }
    if (Utility.isEmpty(svalue)){
      svalue = "";
    }
    try {
      pageContext.getOut().print(this.src());
      return(SKIP_BODY);
    } catch (Exception e) {
      throw new JspException(e);
    }
  }

  public int doEndTag() throws JspException {
    return EVAL_PAGE;
  }
  
  public StringBuffer src() throws JspException{
    HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
    String actionMode = (String)request.getAttribute(GlobalConstant.ACTION_MODE);
    StringBuffer buffer = new StringBuffer();
    StringBuffer sTextBox = new StringBuffer();
    StringBuffer sTextArea = new StringBuffer();
    
    
    if (GlobalConstant.NAV_MODE_NEW.equals(actionMode) || GlobalConstant.NAV_MODE_EDIT.equals(actionMode) || GlobalConstant.NAV_MODE_CHANGE.equals(actionMode) || DmsOperationConstant.CREATE_COMPOUND_DOCUMENT.equals(actionMode)) {
      String boxValue = "";
      String slength = new String(maxlength);
      if (Utility.isEmpty(slength)) {
        slength = GlobalConstant.DEFAULT_LENGTH;
      }
      if (Integer.parseInt(slength) > Integer.parseInt(GlobalConstant.MAX_LENGTH)) {
        slength = GlobalConstant.MAX_LENGTH;
      }
      if (svalue.length() > 0 && (svalue.length() > Integer.parseInt(slength) || svalue.length() > 26)) {
        boxValue = svalue.substring(0, Integer.parseInt(slength)-3) + "...";
      } else if (Utility.isEmpty(svalue)) {
        boxValue = "";
      } else {
        boxValue = svalue;
      }
      
      sTextBox.append("<input ");
      sTextBox.append("type='text' ");
      sTextBox.append("id='" + name + "_box' ");
      if (!Utility.isEmpty(size)) {
        sTextBox.append("size='" + size + "' ");
      }
      if (!Utility.isEmpty(maxlength)) {
        sTextBox.append("maxlength='" + maxlength + "' ");
      }
      sTextBox.append("readonly='true' ");
      sTextBox.append("value='" + boxValue + "' ");
      if (!Utility.isEmpty(boxStyleClass)) {
        sTextBox.append("class='" + boxStyleClass + "' ");
      }
      sTextBox.append("onfocus=\"return boxToArea('" + name + "_box','" + name + "_area','" + name + "')\" ");
      sTextBox.append("/>");
      
      sTextArea.append("<textarea ");
      sTextArea.append("id='" + name + "_area' ");
      if (!Utility.isEmpty(cols)) {
        sTextArea.append("cols='" + cols + "' ");
      }
      if (!Utility.isEmpty(rows)) {
        sTextArea.append("rows='" + rows + "' ");
      }
      if (!Utility.isEmpty(areaStyleClass)) {
        sTextArea.append("class='" + areaStyleClass + "' ");
      }
      sTextArea.append("onblur=\"return areaToBox('" + name + "_box','" + name + "_area','" + name + "','" + maxlength + "')\" ");
      sTextArea.append(" >" + svalue + "</textarea>");
      
      buffer.append("<div>\n");
      buffer.append("  <input id='" + name + "' type='hidden' name='" + name + "' value='" + svalue + "' />\n");
      buffer.append("  <div id='" + name + "_box_tr' ");
      if (!Utility.isEmpty(svalue)){
        buffer.append(" style=\"display:none;\"");
      }
      buffer.append(">\n");
      buffer.append("      " + sTextBox.toString() + "\n");
      buffer.append("  </div>\n");
      buffer.append("  <div id='" + name + "_area_tr'");
      if (Utility.isEmpty(svalue)){
        buffer.append(" style=\"display:none;\"");
      }
      buffer.append(" >\n");
      buffer.append("      " + sTextArea.toString() + "\n");
      buffer.append("  </div>\n");
      buffer.append("</div>\n");
    } else {
      buffer.append("<span>" + svalue + "</span>");
    }
    return buffer;
  }
  
  public void release() {
    super.release();
    maxlength = null;
    name = null;
    readonly = null;
    size = null;
    svalue = null;
    rows = null;
    boxStyleClass = null;
    areaStyleClass = null;
  }
  
  public String getMaxlength() {
    return maxlength;
  }

  public void setMaxlength(String maxlength) {
    this.maxlength = maxlength;
  }
  
  public String getReadonly() {
    return readonly;
  }

  public void setReadonly(String readonly) {
    this.readonly = readonly;
  }

  public String getSize() {
    return size;
  }

  public void setSize(String size) {
    this.size = size;
  }

  public String getRows() {
    return rows;
  }

  public void setRows(String rows) {
    this.rows = rows;
  }

  public String getCols() {
    return cols;
  }

  public void setCols(String cols) {
    this.cols = cols;
  }

  public String getAreaStyleClass() {
    return areaStyleClass;
  }

  public void setAreaStyleClass(String areaStyleClass) {
    this.areaStyleClass = areaStyleClass;
  }

  public String getBoxStyleClass() {
    return boxStyleClass;
  }

  public void setBoxStyleClass(String boxStyleClass) {
    this.boxStyleClass = boxStyleClass;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getSvalue() {
    return svalue;
  }

  public void setSvalue(String svalue) {
    this.svalue = svalue;
  }

}